package com.ai.fzqai;

import static org.junit.jupiter.api.Assertions.assertFalse;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.evaluation.FactCheckingEvaluator;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.document.Document;
import org.springframework.ai.evaluation.EvaluationRequest;
import org.springframework.ai.evaluation.EvaluationResponse;
import org.springframework.ai.openai.OpenAiEmbeddingModel;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.filter.FilterExpressionBuilder;
import org.springframework.ai.zhipuai.ZhiPuAiChatOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.Resource;
import org.springframework.util.CollectionUtils;

import lombok.extern.slf4j.Slf4j;
@SpringBootTest
@AutoConfigureMockMvc
@Slf4j
public class EvaluationIT {
	
	@Qualifier("zhiPuAiChatModel") 
	@Autowired
	private ChatModel zhiPuAiChatModel;
	
	@Autowired 
    private EvaluationTestController testBaen;
	
	
    private static final double SIMILARITY_THRESHOLD = 0.5d;
    private static final int TOP_K = 3;
	@Autowired 
    private OpenAiEmbeddingModel embeddingModel;

	@Test
	public void initTest() {
		
		   VectorStore vectorStore = SimpleVectorStore.builder(embeddingModel).build();
		   var searchDocuments = vectorStore.similaritySearch(
	                SearchRequest.builder()
	                        .filterExpression(
	                        		new FilterExpressionBuilder().eq("title", "中国的首都").build() 
	                        		)
	                        .similarityThreshold(SIMILARITY_THRESHOLD)
	                        .topK(TOP_K)
	                        .build()
	        );
	        if (CollectionUtils.isEmpty(searchDocuments)) {
	            var content = """
	                    中华人民共和国首都位于北京市，中华人民共和国成立前夕的旧称为北平，
	                    是中共中央及中央人民政府所在地，中央四个直辖市之一，
	                    全国政治、文化、国际交往和科技创新中心，中国古都、国家历史文化名城和国家中心城市之一。
	                    """;
	            var document = new Document(content, Map.of("title", "中国的首都"));
	            vectorStore.add(List.of(document));
	        }
	}
	
	
	@Test
	public void RelevancyEvaluator相关性评估器() {
		System.err.println(testBaen.saRelevancy("中国的首都是哪里?"));
	}
	
	@Test
	public void FactCheckingEvaluator事实性评估器() {
		System.err.println(testBaen.saFactChecking("中国的首都是哪里?"));
	}
	
	@Test
	public void AnswerRelevancyEvaluator评分评估器() {
		System.err.println(testBaen.saaAnswerRelevancy("中国的首都是哪里?"));
	}
	
	@Test
	public void AnswerCorrectnessEvaluator正确性评估器() {
		System.err.println(testBaen.saaAnswerCorrectness("中国的首都是哪里?"));
	}
	
	@Test
	public void AnswerFaithfulnessEvaluator评分评估器() {
		System.err.println(testBaen.saaAnswerFaithfulness("中国的首都是哪里?"));
	}
	
	@Test
	public void testFactChecking() {
	  // Set up the Ollama API
//	  OllamaApi ollamaApi = new OllamaApi("http://localhost:11434");
//
//	  ChatModel chatModel = new OllamaChatModel(ollamaApi,
//					OllamaOptions.builder().model(BESPOKE_MINICHECK).numPredict(2).temperature(0.0d).build())


	  // Create the FactCheckingEvaluator
	  var factCheckingEvaluator = new FactCheckingEvaluator(ChatClient.builder(zhiPuAiChatModel)
			  .defaultOptions(ZhiPuAiChatOptions
							.builder()
							.model("glm-4.6")
							.temperature(0d)
							.maxTokens(2)
							.build())
			  );

	  // Example context and claim
	  String context = "The Earth is the third planet from the Sun and the only astronomical object known to harbor life.";
	  String claim = "The Earth is the fourth planet from the Sun.";

	  // Create an EvaluationRequest
	  EvaluationRequest evaluationRequest = new EvaluationRequest(context, Collections.emptyList(), claim);

	  // Perform the evaluation
	  EvaluationResponse evaluationResponse = factCheckingEvaluator.evaluate(evaluationRequest);

	  assertFalse(evaluationResponse.isPass(), "The claim should not be supported by the context");

	}
	
}
